// pages/outline/outline.js
const app = getApp()
const qiniu = require("../../utils/qiniu.min.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    activityProcess:"",
    posterUrl:[],
  },
  //活动详情
  getdata(){
    var that = this
    app.req({
      url:'wechat/activity/activityDetail',
      method: "post",
      data: {
        id: that.data.id,
      },
      success: function (res) {
        if(res.code==200){
          that.setData({
            activityProcess: res.data.activityProcess,
            posterUrl: res.data.posterUrl
          })
          console.log('简介', that.data.activityProcess, that.data.posterUrl)
        }else{
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
  },
  bindTextAreaBlur(e) {
    this.setData({
      activityProcess: e.detail.value,
    })
  },
  delete: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    console.log('删除', index)
    that.remove(that.data.posterUrl, that.data.posterUrl[index]);
    that.setData({
      posterUrl: that.data.posterUrl
    });
    console.log('删除', that.data.posterUrl)
  },
  //删除元素
  remove: function (array, val) {
    for (var i = 0; i < array.length; i++) {
      if (array[i] == val) {
        array.splice(i, 1);
      }
    }
  },
  save(){
    var that = this
    app.req({
      url:'wechat/activity/saveProcess',
      method:'post',
      data:{
        id:that.data.id,
        activityProcess: that.data.activityProcess,
        posterUrl: that.data.posterUrl
      },
      success:function(res){
        if(res.code==200){
          wx.navigateBack({
            delta: 1
          })
          wx.showToast({
            title: '修改成功',
            duration:2000
          })
        }else{
          wx.showToast({
            title: res.msg,
            icon:"none",
            duration: 2000
          })
        }
      }
    })
  },
  //活动概况图片
  posterimg() {
    var that = this;
     if (that.data.posterUrl.length >= 3) {
      wx.showToast({
        title: '最多只能上传3张图片',
        icon: 'none',
        duration: 2000
      })
    } else {
      app.getQiNiuToken(function (res) {
        var countnum = 3 - that.data.posterUrl.length
        var qiniuconfig = res;
        wx.chooseImage({
          count: countnum,
          sizeType: ['original', 'compressed'],
          sourceType: ['album', 'camera'],
          success: function (res) {
            const filepathArray = res.tempFilePaths
            for (let i = 0; i < filepathArray.length; i++) {
              qiniu.upload(filepathArray[i], res => {
                that.setData({
                  posterUrl: that.data.posterUrl.concat([res.imageURL])
                })
                console.log("网络图片", that.data.posterUrl)
              }, (error) => {
                console.log('error' + error)
              }, {
                  uploadURL: 'https://upload.qiniup.com',
                  region: 'ECN',
                  domain: qiniuconfig.qiniuDomain,
                  uptoken: qiniuconfig.upToken,
                })
            }

          }
        })
      });
    }
  },
  //图片点击预览
  previewImg: function (e) {
    // console.log(e.currentTarget.dataset.index);
    var index = e.currentTarget.dataset.index;
    var imgArr = this.data.posterUrl;
    // console.log('图片地址', this.data.act2.posterUrl)
    // console.log(imgArr);
    wx.previewImage({
      current: imgArr[index],     //当前图片地址
      urls: imgArr,               //所有要预览的图片的地址集合 数组形式
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
    this.getdata()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})